package com.ba.base.view.dialog

import android.content.Context
import android.content.DialogInterface
import android.graphics.drawable.AnimationDrawable
import android.widget.ImageView
import android.widget.TextView
import com.ba.base.R
import com.ba.base.view.dialog.BaseDialog

class ProgressDialog : BaseDialog {

    private var tvText: TextView? = null
    private var textResId: Int = 0
    private var text: String? = null
    private lateinit var animationDrawable: AnimationDrawable

    constructor(context: Context) : super(context, R.style.base_dialog)

    constructor(context: Context, themeResId: Int) : super(context, themeResId)

    constructor(context: Context, cancelable: Boolean, cancelListener: DialogInterface.OnCancelListener) :
            super(context, cancelable, cancelListener)

    constructor(context: Context, themeResId: Int, textResId: Int) : super(context, themeResId) {
        this.textResId = textResId
    }

    override fun getContentViewResId(): Int {
        return R.layout.base_dialog_progress
    }

    override fun onSetContentViewAfter() {
        setCanceledOnTouchOutside(false)
        super.onSetContentViewAfter()
    }

    override fun initView() {
        tvText = findViewById(R.id.tv_text)
        if (textResId != 0) {
            tvText?.setText(textResId)
        }
        if (text != null) {
            tvText?.text = text
        }
        val loading_img: ImageView = findViewById(R.id.loading_img)
        animationDrawable = loading_img.drawable as AnimationDrawable
        animationDrawable.start()
    }

    fun setText(textResId: Int) {
        if (tvText != null) {
            tvText?.setText(textResId)
        } else {
            this.textResId = textResId
        }
    }

    fun setText(text: String) {
        if (tvText != null) {
            tvText?.text = text
        } else {
            this.text = text
        }
    }
}